package com.yanqu.road.server.cmd.wingroom;

import com.yanqu.road.entity.wingroom.WingRoomRegion;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.wingroom.WingRoomMgr;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;

/**
 * @author ：zfr
 * @date ：2022/3/21 10:29
 */
@Cmd(code = GameProtocol.S_WING_ROOM_CROSS_COPY_PLAN, desc = "跨服来拷贝方案")
public class CrossCopyPlanCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        WingRoomProto.GetOtherPlanCrossReqMsg crossReqMsg = WingRoomProto.GetOtherPlanCrossReqMsg.parseFrom(packet.getBytes());
        WingRoomProto.GetOtherPlanRespMsg.Builder respMsg = WingRoomProto.GetOtherPlanRespMsg.newBuilder();
        respMsg.setServerId(GameServer.getInstance().getServerId());
        respMsg.setRet(0);
        WingRoomProto.GetOtherPlanRespCrossMsg.Builder respCrossMsg = WingRoomProto.GetOtherPlanRespCrossMsg.newBuilder();
        Map<Integer, WingRoomRegion> planRegion = WingRoomMgr.copyPlan(packet.getPlayerId(), Long.parseLong(crossReqMsg.getPlanUid()), respMsg);
        if (planRegion == null) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_COPY_PLAN_NO_FOUND);
        } else {
            for (WingRoomRegion region : planRegion.values()) {
                respCrossMsg.addRegion(WingRoomPb.buildWingRoomRegionTempMsg(region));
            }
        }
        respCrossMsg.setRespMsg(respMsg);
        respCrossMsg.setPlanName(crossReqMsg.getPlanName());
        respCrossMsg.setOriServerId(crossReqMsg.getRespServerId());
        respCrossMsg.setOriUserId(crossReqMsg.getRespUserId());
        YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_WING_ROOM_COPY_OTHER_PLAN_RESP, respCrossMsg);
        GamePlayerMgr.sendPacket(0, message);
    }
}
